//
//  VedioMsgCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/22.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "VideoMsgCell.h"

@implementation VideoMsgCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    self.vedioImgView = [UIImageView new];
    self.vedioImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.playBtn = UIButton.k_initType(UIButtonTypeCustom).k_img(UIImage.k_imgName(@"msg_video"));
    
    [self.msgContentView addSubview:self.vedioImgView];
    [self.msgContentView addSubview:self.playBtn];
    
    UIImage *img = UIImage.k_imgName(@"msg_placeholder_video");
    self.vedioImgView.image = img;
    CGFloat maxSize = ScreenWidth/3;
    CGFloat width = img.size.width;
    CGFloat height = img.size.height;
    if (width > height) {
        height = maxSize/width * height;
        width = maxSize;
        if (height < maxSize/3) {
            height = maxSize/3;
        }
    } else {
        width = maxSize/height * width;
        height = maxSize;
        if (width < maxSize/3) {
            width = maxSize/3;
        }
    }
    [self.vedioImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.msgContentView);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
    }];
    
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.vedioImgView);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
}

- (void)setMsgModel:(MsgModel *)msgModel {
    [super setMsgModel:msgModel];
    self.msg = msgModel.msg;
}
- (void)setMsg:(MsgItem *)msg {
    _msg = msg;
    
    
    NSData *data = [[NSData alloc] initWithBase64EncodedString:[NSString kk_safeString:msg.s_image] options:NSDataBase64DecodingIgnoreUnknownCharacters];
    self.vedioImgView.image = data.length > 0 ? [UIImage imageWithData:data]:UIImage.k_imgName(@"msg_placeholder_video");
}

@end
